1 : <?php
2 : /*--------------------------------------------------------------------------+
3 : This file is part of eStudy
4 : externaltools/classes/class.mantisinfo.inc.php
5 : - Modulgruppe: ExternalTools
6 : - Beschreibung: Diese Datei enthällt die Klasse MantisInfo.
7 : - Version: 0.1 23/04/05
8 : - Autor(en): Christian Gerhardt <case42@gmx.net>
9 : +---------------------------------------------------------------------------+
10 : This program is free software; you can redistribute it and/or
11 : modify it under the terms of the GNU General Public License
12 : as published by the Free Software Foundation; either version 2
13 : of the License, or any later version.
14 : +---------------------------------------------------------------------------+
15 : This program is distributed in the hope that it will be useful,
16 : but WITHOUT ANY WARRANTY; without even the implied warranty of
17 : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 : GNU General Public License for more details.
19 : You should have received a copy of the GNU General Public License
20 : along with this program; if not, write to the Free Software
21 : Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22 : +--------------------------------------------------------------------------*/
23 : /**
24 : * Diese Datei enthällt die Klasse MantisInfo.
25 : * @package eStudy.ExternalTools
26 : * @version 1.0 13/10/05
27 : * @author Christian Gerhardt <case42@gmx.net>
28 : */
29 :
30 1 : if (!defined('PATH_TO_ROOT'))
31 1 : define("PATH_TO_ROOT", "../../");
32 :
33 : /**Die Basisklasse*/
34 1 : require_once ("class.toolinfo.inc.php");
35 : /*für Passwort-behandlung*/
36 1 : require_once ("class.passwords.inc.php");
37 : /**Die Tabelle in der Mantis seine User verwaltet.*/
38 1 : define("MANTIS_USER_TABLE", "mantis_user_table");
39 : /**Die Tabelle in der Mantis seine Projekte verwaltet.*/
40 1 : define("MANTIS_PROJECTS_TABLE", "mantis_project_table");
41 1 : define("MANTIS_PROJECTS_USER_TABLE", "mantis_project_user_list_table");
42 1 : define("MANTIS_PROFILE_TABLE", "mantis_user_profile_table");
43 1 : define("MANTIS_PROJECT_USERLIST_TABLE", "mantis_project_user_list_table");
44 1 : define("MANTIS_PREFERENCE_TABLE", "mantis_user_pref_table");
45 1 : define("MANTIS_VIEWER_ACCESSLEVEL", 10);
46 1 : define("MANTIS_REPORTER_ACCESSLEVEL", 25);
47 1 : define("MANTIS_DEVELOPER_ACCESSLEVEL", 55);
48 : /**
49 : * Die Rechte eines Mantis Admins.
50 : * Wird als Standardwert für eStudy-Dozenten verwendet.
51 : */
52 1 : define("MANTIS_ADMIN_USER_RIGHTS", 90);
53 : /**
54 : * Die Standardrechte für einen Mantisbenutzer.
55 : * Wird als Standardwert für eStudy-Studenten verwendet.
56 : */
57 1 : define("MANTIS_DEFAULT_USER_RIGHTS", MANTIS_REPORTER_ACCESSLEVEL);
58 : /**
59 : * Toolinfo Klasse für Mantis in der Version 1.x ..
60 : * @package eStudy.ExternalTools
61 : * @version 1.0 13/10/05
62 : * @author Christian Gerhardt <case42@gmx.net>
63 : */
64 1 : class MantisInfo extends ToolInfo {
65 : /**
66 : * Der von Mantis benutzte CookieString.
67 : * @access private
68 : * @var String
69 : */
70 : public $cookieStrings = null;
71 : public $strings;
72 :
73 : /**
74 : * Erzeugt ein <code>MantisInfo</code> Objekt.
75 : * @param integer $id
76 : * @param String $image
77 : */
78 : public function MantisInfo($id, $image = null) {
79 0 : parent :: ToolInfo($image);
80 0 : $this->toolName = "Mantis Bugtracker 1.x";
81 0 : $this->toolID = $id;
82 0 : $this->strings['label_create_projects'] = "Automatisch ein Projekt für den Kurs anlegen.";
83 0 : $this->strings['info_create_projects'] = "Alle Kursteilnehmer werden automatisch als Developer im Projekt aufgenommen.";
84 0 : $this->strings['default_project_description'] = "Von eStudy automatisch generiertes Projekt des Kurses \"%s\"";
85 0 : }
86 : /**
87 : * Prüft ob der aktuelle eStudy-Benutzer bereits in die
88 : * Mantis Benutzertabelle aufgenommen worden ist.
89 : * @access public
90 : * @return bool <i>true</i>-Falls der Benutzer bereits eingetragen ist.
91 : */
92 : public function checkForUser() {
93 0 : $query = "SELECT * FROM %s WHERE username = '%s' LIMIT 1";
94 0 : $query = sprintf($query, MANTIS_USER_TABLE, $this->userLogonData['login']);
95 0 : $result = $this->toolDB->get_row($query);
96 0 : if (count($result) > 0) {
97 0 : return true;
98 : } else {
99 0 : return false;
100 : }
101 : }
102 : /**
103 : * Fügt den aktuellen e-Study-Benutzer in die Mantisbenutzertabelle ein.
104 : * @access public
105 : * @return void
106 : */
107 : public function insertNewUser() {
108 0 : $cookie_string = $this->auth_generate_unique_cookie_string();
109 0 : $query = "INSERT INTO %s ";
110 0 : $query .= "(`username`, `realname`, `email`, `date_created`, `password`,`access_level`,`cookie_string`) ";
111 0 : $query .= "VALUES('%s','%s','%s','%s','%s',%s,'%s')";
112 0 : $query = sprintf($query, MANTIS_USER_TABLE, $this->userLogonData['login'], $this->userData['Vorname'] . " " . $this->userData['Nachname'], $this->userData['Email'], date("Y-m-d H:i:s"), Passwords :: GenerateHash($this->userLogonData['password'], "", HASH_MANTIS), $this->getMantisAccessLevel($this->userData['Usergroup'], (bool) $this->userData['Invisible']), $cookie_string);
113 0 : $this->toolDB->query($query);
114 0 : }
115 : /*übernommen von mantis bugtracker authentication_api.php*/
116 : /**
117 : * Generiert einen Mantis-Cookiestring.
118 : * @access private
119 : * @return String - der Cookiestring.
120 : */
121 : public function auth_generate_cookie_string() {
122 0 : $t_val = mt_rand(0, mt_getrandmax()) + mt_rand(0, mt_getrandmax());
123 0 : $t_val = md5($t_val) . md5(time());
124 0 : return substr($t_val, 0, 64);
125 : }
126 : /*übernommen von mantis bugtracker authentication_api.php*/
127 : /**
128 : * Generiert einen einmaligen Cookiestring.
129 : * @access private
130 : * @return String - der einmalige Cookiestring.
131 : */
132 : public function auth_generate_unique_cookie_string() {
133 : do {
134 0 : $t_cookie_string = $this->auth_generate_cookie_string();
135 0 : } while (!$this->auth_is_cookie_string_unique($t_cookie_string));
136 0 : return $t_cookie_string;
137 : }
138 : /*eigen konstruktion*/
139 : /**
140 : * Prüft ob der Cookiestring einmalig ist.
141 : * @param String $cookie_string - der Cookiestring der überprüft werden soll.
142 : * @param Bool <i>true</i> wenn der Cookiestring einmalig ist.
143 : */
144 : public function auth_is_cookie_string_unique($cookie_string) {
145 0 : if (is_null($this->cookieStrings)) {
146 0 : $query = "SELECT cookie_string FROM %s";
147 0 : $query = sprintf($query, MANTIS_USER_TABLE);
148 0 : $this->cookieStrings = $this->toolDB->get_col($query);
149 0 : }
150 0 : return !is_array($this->cookieStrings) || !in_array($cookie_string, $this->cookieStrings);
151 : }
152 : /**
153 : * Gibt die Mantisbenutzerrechte für eine bestimmte eStudy-Benutzergruppe zurück.
154 : * @access public
155 : * @param integer $usergroup - die eStudy-Benutzergruppe.
156 : * @param boolean $invisible - Benutzer ist unsichtbar oder nicht
157 : * @return integer - die Mantisbenutzerrechte.
158 : */
159 : public function getMantisAccessLevel($usergroup, $invisible = false) {
160 : switch ($usergroup) {
161 0 : case ADMIN :
162 0 : return MANTIS_ADMIN_USER_RIGHTS;
163 0 : default :
164 : if ($invisible)
165 0 : return MANTIS_VIEWER_ACCESSLEVEL;
166 0 : return MANTIS_DEFAULT_USER_RIGHTS;
167 0 : }
168 : }
169 : /**
170 : * Aktualisiert die Mantisbenutzertabelle, nachdem der
171 : * eStudy-Benutzer innerhalb von eStudy seine Logindaten geändert hat.
172 : * @access public
173 : * @param String $oldLogin - Der alte Loginname des Benutzers.
174 : * @param String $newLogin - Der neue Loginname des Benutzers.
175 : * @param String $password - Das Passwort des Benutzers.
176 : * @return bool - <i>true</i> wenn alles geklappt hat.
177 : */
178 : public function updateLogin($oldLogin, $newLogin, $password) {
179 0 : global $EZSQL_ERROR;
180 0 : if ($this->checkForUser()) {
181 0 : $query = "UPDATE %s SET username='%s', password='%s' WHERE username='%s'";
182 0 : $query = sprintf($query, MANTIS_USER_TABLE, $newLogin, Passwords :: GenerateHash($password, "", HASH_MANTIS), //md5($password)
183 0 : $oldLogin);
184 0 : $this->toolDB->query($query);
185 : if ($EZSQL_ERROR)
186 0 : return false;
187 0 : } else {
188 0 : $this->insertNewUser();
189 : }
190 0 : return true;
191 : }
192 : /**
193 : *
194 : * @access public
195 : * @param array $hiddenVars
196 : * @return
197 : */
198 : public function showOptionsDialog($hiddenVars) {
199 0 : $result = $this->checkPostVars();
200 0 : if ($result === false) {
201 0 : $this->echoDialog($hiddenVars);
202 0 : }
203 0 : return $result;
204 : }
205 : /**
206 : *
207 : * @access private
208 : * @return mixed
209 : */
210 : public function checkPostVars() {
211 0 : if (isset ($_POST['optionsDialog'])) {
212 0 : if (isset ($_POST['okButton'])) {
213 0 : $result = array ();
214 0 : if (isset ($_POST['create_projects'])) {
215 0 : $result['create_projects'] = true;
216 0 : } else {
217 0 : $result['create_projects'] = false;
218 : }
219 0 : return $result;
220 : }
221 0 : return true;
222 : }
223 0 : return false;
224 : }
225 : /**
226 : *
227 : * @access private
228 : * @param array $hiddenVarsm
229 : * @return
230 : */
231 : public function echoDialog($hiddenVars) {
232 0 : $inputString = "<p class='pForm'><input type='%s' name='%s' value='%s' %s /></p>\n";
233 0 : printf("<form method='post' action='%s'>\n", PATH_TO_ROOT . SCRIPT_NAME);
234 0 : echo "<table class='tableBorder'>\n";
235 0 : Output :: echoTableHead("Spezielle Einstellungen von " . $this->toolName, 2);
236 0 : echo "<tr><td colspan='2'>\n";
237 0 : printf($inputString, "hidden", "optionsDialog", "true", "");
238 0 : foreach ($hiddenVars as $key => $value) {
239 0 : printf($inputString, "hidden", $key, $value, "");
240 0 : }
241 0 : echo $this->strings['label_create_projects'];
242 0 : if ($this->toolOptions['create_projects'] == true) {
243 0 : printf($inputString, "checkbox", 'create_projects', 'true', "checked='checked'");
244 0 : } else {
245 0 : printf($inputString, "checkbox", 'create_projects', 'false', "");
246 : }
247 0 : echo "<br />";
248 0 : echo $this->strings['info_create_projects'];
249 0 : echo "</td></tr>\n";
250 0 : Output :: echoDialogBottom(null, 2);
251 0 : echo "</table>\n";
252 0 : echo "</form>\n";
253 0 : }
254 : /**
255 : * Handelt spezielle Events ab, auf die mit den ToolOptionen reagiert werden kann.
256 : * <p>
257 : * Auf <code>OPTIONS_EVENT_SHOW_PAGE</code> wird reagiert
258 : * in dem ein neues Projekt mit dem Kursnamen angelegt wird,
259 : * und der aktuelle Benutzer in dieses Projekt eingetragen wird.
260 : * </p>
261 : * @access public
262 : * @param integer $event - der Event der behandelt werden muss.
263 : * @return void
264 : */
265 : public function optionsHandler($event) {
266 0 : global $db, $settings;
267 : switch ($event) {
268 0 : case OPTIONS_EVENT_SHOW_PAGE :
269 0 : if ($_SESSION["course"] > 0) {
270 0 : $query = "SELECT Name FROM %scourses WHERE ID='%s'";
271 0 : $query = sprintf($query, $settings['dbPrefix'], $_SESSION['course']);
272 0 : $courseName = $db->get_var($query);
273 0 : } else {
274 0 : $courseName = "eStudy";
275 : }
276 0 : if (!empty ($courseName)) {
277 0 : $projectID = $this->createProject($courseName);
278 0 : $this->assignToProject($projectID);
279 0 : }
280 0 : break;
281 : }
282 0 : }
283 : /**
284 : * Erzeugt ein Mantis-Projekt.
285 : * Falls ein Projekt mit dem übergebenen Namen bereits exisitiert,
286 : * wird nur ID dieses Projekts zurückgegeben.
287 : *
288 : * @access public
289 : * @param String $projectName
290 : * @return integer - die ID des neu erzeugten Projekts.
291 : */
292 : public function createProject($projectName) {
293 0 : $query = "SELECT id FROM %s WHERE name='%s'";
294 0 : $query = sprintf($query, MANTIS_PROJECTS_TABLE, Data :: toMysql($projectName));
295 0 : $projectID = $this->toolDB->get_var($query);
296 0 : if (!(bool) $projectID) {
297 0 : $query = "INSERT INTO %s (name, status, enabled, view_state, access_min, file_path, description) ";
298 0 : $query .= "VALUES('%s', '%s', '%s', '%s', '%s', '%s', '%s')";
299 0 : $query = sprintf($query, MANTIS_PROJECTS_TABLE, Data :: toMysql($projectName), 10, 1, 50, 10, "", sprintf(Data :: toMysql($this->strings['default_project_description']), Data :: toMysql($projectName)));
300 0 : $this->toolDB->query($query);
301 0 : $projectID = $this->toolDB->insert_id;
302 0 : }
303 0 : return $projectID;
304 : }
305 : /**
306 : * Füg den aktuellen eStudy-Benutzer einem Mantis-Projekt hinzu,
307 : * falls dieser noch nicht Mitglied ist.
308 : * @access public
309 : * @param integer $projectID - die ID des Mantis-Projekts.
310 : * @return void
311 : */
312 : public function assignToProject($projectID) {
313 0 : $query = "SELECT id FROM %s WHERE username = '%s' LIMIT 1";
314 0 : $query = sprintf($query, MANTIS_USER_TABLE, $this->userLogonData['login']);
315 0 : $userID = $this->toolDB->get_var($query);
316 0 : $query = "SELECT * FROM %s WHERE project_id='%s' AND user_id='%s'";
317 0 : $query = sprintf($query, MANTIS_PROJECTS_USER_TABLE, (int) $projectID, (int) $userID);
318 0 : $project_data = $this->toolDB->get_row($query);
319 0 : $accessLevel = MANTIS_REPORTER_ACCESSLEVEL;
320 0 : if ($_SESSION["course"] > 0)
321 0 : $accessLevel = MANTIS_DEVELOPER_ACCESSLEVEL;
322 0 : if ($this->userData['Invisible'])
323 0 : $accessLevel = MANTIS_VIEWER_ACCESSLEVEL;
324 0 : if (!$project_data) {
325 0 : $query = "INSERT INTO %s SET project_id='%s', user_id='%s', access_level='%s'";
326 0 : $query = sprintf($query, MANTIS_PROJECTS_USER_TABLE, (int) $projectID, (int) $userID, Data :: toMysql($accessLevel));
327 0 : $this->toolDB->query($query);
328 0 : }
329 0 : }
330 : public function removeUser($userID) {
331 0 : $this->getLogonData();
332 0 : $this->connectToDB();
333 0 : $this->getUserLoginData($userID);
334 0 : if ($this->checkForUser()) {
335 0 : $query = "SELECT id FROM %s WHERE username='%s'";
336 0 : $query = sprintf($query, MANTIS_USER_TABLE, $this->userLogonData['login']);
337 0 : $mantisUserID = $this->toolDB->get_var($query);
338 : //profiles löschen
339 0 : $query = "DELETE FROM %s WHERE user_id='%s'";
340 0 : $query = sprintf($query, MANTIS_PROFILE_TABLE, Data :: toMysql($mantisUserID));
341 0 : $this->toolDB->query($query);
342 : //preferencedelete
343 0 : $query = "DELETE FROM %s WHERE user_id='%s'";
344 0 : $query = sprintf($query, MANTIS_PREFERENCE_TABLE, Data :: toMysql($mantisUserID));
345 0 : $this->toolDB->query($query);
346 : //user_delete_project_specific_access_levels
347 0 : $query = "DELETE FROM %s WHERE user_id='%s'";
348 0 : $query = sprintf($query, MANTIS_PROJECT_USERLIST_TABLE, Data :: toMysql($mantisUserID));
349 0 : $this->toolDB->query($query);
350 : //delete user
351 0 : $query = "DELETE FROM %s WHERE id='%s'";
352 0 : $query = sprintf($query, MANTIS_USER_TABLE, Data :: toMysql($mantisUserID));
353 0 : $this->toolDB->query($query);
354 0 : }
355 0 : }
356 : }
|